package com.A1908.servlet;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import com.A1908.entity.Captcha;
import com.A1908.entity.User;
import com.A1908.service.UserService;
import com.A1908.service.impl.UserServiceImpl;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

import com.A1908.util.MD5util;
import com.A1908.util.MyFinal;
import com.alibaba.fastjson.JSON;

@WebServlet(urlPatterns = {"/user"})
public class UserServlet extends HttpServlet {
    UserService userService=new UserServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        String opr = req.getParameter("opr");
        User user = null;
        switch (opr){
            case"login":
                user=new User();
                user.setPhoneNumber((req.getParameter("username")));
                user.setPassword(MD5util.encrypt(req.getParameter("password")));
                PrintWriter printWriter=resp.getWriter();
                User result=new User();
                result=userService.findUser(user);
                if (result!=null){
                    req.getSession().setAttribute("userID",result.getId());
                    req.getSession().setAttribute("userEmail",result.getUserEmail());
                    req.getSession().setAttribute("userinfo",JSON.toJSONString(result));
                    printWriter.print(JSON.toJSONString(result));
                }
                else {
                    printWriter.print(JSON.toJSONString(new User()));
                }
                break;
            case"add":
                user = new User();
                user.setUsername("邮箱用户");
                user.setPassword(MD5util.encrypt("admin"));
                user.setCreatDate(req.getParameter("creatDate"));
                user.setPhoneNumber("000");
                user.setWebIsOnline(0);
                user.setAndroidIsOnline(0);
                user.setUserImg("");
                user.setUserEmail(req.getParameter("email"));
                String getCaptcha1=req.getParameter("myCaptcha");
                PrintWriter pw2=resp.getWriter();
                if (getCaptcha1.equals(req.getSession().getAttribute("captcha"))){
                    int count = userService.addUser(user);
                    if (count > 0){
                        pw2.print("success");
                    }else {
                        pw2.print("file");
                    }
                }else {
                    pw2.print("file");
                }

                break;
            case "getUser":
                int id = (int) req.getSession().getAttribute("userID");
                user = userService.findUserById(id);
                resp.getWriter().print(JSON.toJSONString(user));
                break;
            case "logout":
                HttpSession session = req.getSession();
                session.invalidate();
//                resp.sendRedirect("index.jsp");
                resp.getWriter().print(JSON.toJSONString(null));
                break;
            case "getCaptcha":
                try{
                    LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 100);
                    String str=lineCaptcha.getImageBase64Data();

                    Captcha captcha=new Captcha();
                    captcha.setCaptchaValue(lineCaptcha.getCode());
                    captcha.setBase64Img(lineCaptcha.getImageBase64Data());
                    resp.getWriter().print(JSON.toJSONString(captcha));
                }catch (Exception e){
                    e.printStackTrace();
                }
                break;
            case "editSave":
                user = new User();
                user = userService.findUserById(Integer.parseInt(req.getParameter("id")));
                user.setId(Integer.parseInt(req.getParameter("id")));
                user.setUsername(req.getParameter("userName"));
                user.setCreatDate(req.getParameter("creatDate"));
                user.setPhoneNumber(req.getParameter("phone"));
                user.setUserImg(req.getParameter("userImg"));
                user.setUserEmail(req.getParameter("userEmail"));
                int count = userService.editUser(user);

                req.getSession().removeAttribute("userinfo");
                req.getSession().setAttribute("userinfo",JSON.toJSONString(user));

                if (count > 0){
                    resp.getWriter().print("true");
                }else {
                    resp.getWriter().print("false");
                }
                break;
            case "loginByCaptcha":
                String getCaptcha=req.getParameter("captcha");
                PrintWriter printWriter2=resp.getWriter();
                if (getCaptcha.equals(req.getSession().getAttribute("captcha"))){
                    user=new User();
                    user.setUserEmail(req.getParameter("email"));
                    User result2=new User();
                    result=userService.findUser(user);
                    if (result!=null){
                        req.getSession().setAttribute("userID",result.getId());
                        req.getSession().setAttribute("userinfo",JSON.toJSONString(result));
                        req.getSession().setAttribute("userEmail",result.getUserEmail());
                        printWriter2.print(JSON.toJSONString(result));
                    }
                    else {
                        printWriter2.print(JSON.toJSONString(new User()));
                    }
                }else {
                    printWriter2.print(JSON.toJSONString(new User()));
                }
                break;
            case "userNameVerify":
                UserServiceImpl userService=new UserServiceImpl();
                User u=new User();
                u.setPhoneNumber(req.getParameter("userName"));
                u.setUserEmail(req.getParameter("userEmail"));
                boolean rs=userService.userVerify(u);
                PrintWriter pw=resp.getWriter();
                if (!rs){
                    pw.print("false");
                }else {
                    pw.print("ture");
                }
                break;
            case"changePwd":
                PrintWriter pw3=resp.getWriter();
                String getCaptcha2=req.getParameter("captcha");
                if (getCaptcha2.equals(req.getSession().getAttribute("captcha"))){
                    UserServiceImpl userService1=new UserServiceImpl();
                    User uchange=new User();
                    if (req.getParameter("tag").equals("getSessionEmail")){
                        uchange.setUserEmail((String) req.getSession().getAttribute("userEmail"));
                    }else {
                        uchange.setUserEmail(req.getParameter("userEmail"));
                    }
                    User uchange2=new User();
                    uchange2=userService1.findUser(uchange);
                    uchange2.setPassword(MD5util.encrypt(req.getParameter("newPassword")));
                    int count3 =  userService1.editUser(uchange2);
                    if (count3 > 0){
                        pw3.print("success");
                    }else {
                        pw3.print("failed");
                    }
                }else {
                    pw3.print("failed");
                }
                break;
        }
    }
}
